using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;

using Deepcode.Flux.Core.Services.XML;
using Deepcode.Flux.Core.Systems.Workflow.Bases;
using Deepcode.Flux.Core.Systems.Workflow.Interfaces;

namespace Deepcode.Flux.Core.Systems.Workflow.BaseHandlers
{
	/// <summary>
	/// Exposes the configuration form for the send an email standard workflow action
	/// </summary>
	public class SendAnEmailConfig : IWorkflowHandlerMethodConfig
	{
		/// <summary>
		/// Will hold the controls for the options
		/// </summary>
		ArrayList controls = new ArrayList();
		
		// Control declarations
		IWorkflowConfigControl HostedControl;

		/// <summary>
		/// Build the configuration form
		/// </summary>
		/// <param name="HandlerID"></param>
		/// <param name="DefinitionID"></param>
		/// <param name="ProcessID"></param>
		/// <returns></returns>
		public System.Collections.ArrayList AddFormControls(System.Web.UI.Page CallingPage, string HandlerID, int DefinitionID, int ProcessID)
		{
			controls.Clear();

			// Get and load the control
			try
			{
				System.Web.UI.Control c = CallingPage.LoadControl("~/admin/Private/Config/Workflow/SendEmailConfig.ascx");
				if (c is IWorkflowConfigControl)
				{
					HostedControl = (IWorkflowConfigControl)c;
					controls.Add(c);
					HostedControl.SetWorkflow( HandlerID, DefinitionID, ProcessID );
				}
				else throw new Exception("Not IWorkflowConfigControl");
			}
			catch 
			{ 
				Label l = new Label();
				l.ForeColor = System.Drawing.Color.Red;
				l.Font.Bold = true;
				l.Text = "Config control is not an IWorkflowConfigControl";
				controls.Add( l );
			}
			return controls;
		}
		/// <summary>
		/// Serialise the configuration form to XML
		/// </summary>
		/// <returns></returns>
		public string Serialise()
		{
			if( HostedControl != null )
				return HostedControl.Serialise();
			
			return "";
		}
		/// <summary>
		/// Deserialise the XML to the configuration form
		/// </summary>
		/// <param name="XmlDatapacket"></param>
		public void DeSerialise(string XmlDatapacket)
		{
			if( HostedControl != null )
				HostedControl.DeSerialise( XmlDatapacket );
		}
	}
}
